package com.spring.pick2pick;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.pick2pick.dao.LeagueDao;
import com.spring.pick2pick.dao.TeamDao;
import com.spring.pick2pick.model.LeagueItem;
import com.spring.pick2pick.model.TeamItem;

@Controller
public class AjaxController {
	
	@Autowired
	LeagueDao leagueDao;
	@Autowired
	TeamDao teamDao;
	
	/** 리그 리스트 목록 */
	@RequestMapping(value = "league", method = RequestMethod.GET)
	public String league(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		String sportStr = request.getParameter("sport");
		
		if (sportStr != null) {
			
			if (sportStr.equals("default")) {
				try {
					String html = "<?xml version='1.0' encoding='utf-8' ?>";
					html += "<league>";
					html += "<item>";
					html += "<idLeague>default</idLeague>";
					html += "<leagueName>- 종목을 먼저 선택하여 주세요 -</leagueName>";
					html += "</item>";
					html += "</league>";
					response.setCharacterEncoding("utf-8");
					PrintWriter out = response.getWriter();
					out.print(html);
				} catch (IOException e) {
					e.printStackTrace();
				}
				return null;
			} else {
				int idSport = Integer.parseInt(sportStr);
				List<LeagueItem> list = leagueDao.getLeagueList(1, -1, idSport, "l.leagueName ASC");
				
				if (list == null || list.size() == 0) {
					try {
						String html = "<?xml version='1.0' encoding='utf-8' ?>";
						html += "<league>";
						html += "<item>";
						html += "<idLeague>default</idLeague>";
						html += "<leagueName>- 해당 종목의 리그없음 -</leagueName>";
						html += "</item>";
						html += "</league>";
						response.setCharacterEncoding("utf-8");
						PrintWriter out = response.getWriter();
						out.print(html);
					} catch (IOException e) {
						e.printStackTrace();
					}
					return null;
				} else {
					model.addAttribute("list", list);
				}
				
			}
		}
		
		return "ajax/league";
	}
	
	/** 리그 리스트 목록 */
	@RequestMapping(value = "team", method = RequestMethod.GET)
	public String team(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		String leagueStr = request.getParameter("league");
		
		if (leagueStr != null) {
			
			if (leagueStr.equals("default")) {
				try {
					String html = "<?xml version='1.0' encoding='utf-8' ?>";
					html += "<team>";
					html += "<item>";
					html += "<idTeam>default</idTeam>";
					html += "<teamName>- 리그를 먼저 선택하여 주세요 -</teamName>";
					html += "</item>";
					html += "</team>";
					response.setCharacterEncoding("utf-8");
					PrintWriter out = response.getWriter();
					out.print(html);
				} catch (IOException e) {
					e.printStackTrace();
				}
				return null;
			} else {
				int idLeague = Integer.parseInt(leagueStr);
				List<TeamItem> list = teamDao.getTeamList(1, -1, " AND t.idLeague=" + idLeague, "t.teamName ASC");
				
				if (list == null || list.size() == 0) {
					try {
						String html = "<?xml version='1.0' encoding='utf-8' ?>";
						html += "<team>";
						html += "<item>";
						html += "<idTeam>default</idTeam>";
						html += "<teamName>- 해당 종목의 팀없음 -</teamName>";
						html += "</item>";
						html += "</team>";
						response.setCharacterEncoding("utf-8");
						PrintWriter out = response.getWriter();
						out.print(html);
					} catch (IOException e) {
						e.printStackTrace();
					}
					return null;
				} else {
					model.addAttribute("list", list);
				}
				
			}
		}
		
		return "ajax/team";
	}

}
